package com.my.commonlibrary.widget;

import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.my.commonlibrary.R;
import com.my.commonlibrary.utils.ToastUtil;
import com.my.commonlibrary.widget.jumpingbeans.JumpingBeans;

/**
 * Author : Cooper
 * Time : 2016/9/29  10:02
 * Description : 对话框集合
 */

public class DialogFactory {

    public static ProgressDialog createProgressDialog(Context context) {
        ProgressDialog dialog = new ProgressDialog(context);
        dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        dialog.setMax(100);
        dialog.setMessage("正在加载...");
//        dialog.setButton(ProgressDialog.BUTTON_POSITIVE, "确定", new DialogInterface.OnClickListener() {
//            @Override
//            public void onClick(DialogInterface dialogInterface, int i) {
//                ToastUtil.showToast("确定");
//            }
//        });
//        dialog.setButton(ProgressDialog.BUTTON_NEGATIVE, "取消", new DialogInterface.OnClickListener() {
//            @Override
//            public void onClick(DialogInterface dialogInterface, int i) {
//                ToastUtil.showToast("取消");
//            }
//        });
//        dialog.setButton(ProgressDialog.BUTTON_NEUTRAL, "中立", new DialogInterface.OnClickListener() {
//            @Override
//            public void onClick(DialogInterface dialogInterface, int i) {
//                ToastUtil.showToast("中立");
//            }
//        });

        return dialog;
    }

    /**
     * 创建dialog基本方法
     * @param context
     * @param view
     * @param flag    false表示屏蔽返回键
     * @return
     */
    public static Dialog createDialog(Context context, View view, boolean flag) {

        Dialog dialog = new Dialog(context, R.style.myStyleDialog);
        dialog.setCanceledOnTouchOutside(flag);
        dialog.setCancelable(flag);
        dialog.setContentView(view, new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.MATCH_PARENT));
        return dialog;
    }

    /**
     * 知道了对话框
     *
     * @param context
     * @param content  显示的内容
     * @param flag     触摸或者返回关闭dialog
     * @param iscenter 文字是否居中
     * @return
     */
    public static Dialog showKnowDialog(Context context, String content, boolean iscenter, boolean flag) {
        View view = View.inflate(context, R.layout.dialog_know, null);
        final Dialog dialog = createDialog(context, view, flag);
        TextView tv_msg = (TextView) view.findViewById(R.id.tv_msg);
        if (iscenter) {
            tv_msg.setGravity(Gravity.CENTER);
        } else {
            tv_msg.setGravity(Gravity.NO_GRAVITY);
        }
        tv_msg.setText(content);
        TextView confirm = (TextView) view.findViewById(R.id.confirm);
        confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        dialog.show();
        return dialog;
    }

    /**
     * 加载dialog
     * @param context
     * @return
     */
    public static Dialog showNewLoading(Context context) {
        final Dialog dialog = new Dialog(context, R.style.MyDialogStyle);
        dialog.setCancelable(false);
        Window window = dialog.getWindow();
        window.setContentView(R.layout.dialog_loading3_layout);

        WindowManager.LayoutParams params = window.getAttributes();
        int widthPixels = context.getResources().getDisplayMetrics().widthPixels;
        params.width = widthPixels / 10 * 8;
        window.setAttributes(params);

        final ImageView iv_loading = (ImageView) window.findViewById(R.id.iv_loading);
        final Animation animationUtils = AnimationUtils.loadAnimation(context, R.anim.loading_circle_new);
        iv_loading.startAnimation(animationUtils);
        TextView tv_content = (TextView) window.findViewById(R.id.tv_content);
        TextView tv_dismiss = (TextView) window.findViewById(R.id.tv_dismiss);
        final JumpingBeans jumpingBeans = JumpingBeans.with(tv_content).appendJumpingDots().build();

        tv_dismiss.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (dialog.isShowing()) {
                    iv_loading.clearAnimation();
                    animationUtils.cancel();
                    jumpingBeans.stopJumping();
                    dialog.dismiss();
                }
            }
        });

        dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {
                iv_loading.clearAnimation();
                animationUtils.cancel();
                jumpingBeans.stopJumping();
            }
        });


        return dialog;

    }


}
